package com.iris.live.services.report.sources;

import com.iris.live.services.common.GetConfigureInfoUtils;
import com.iris.live.services.data.repositories.DataCheckRepository;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2017/5/25.
 */
@Component("ExportDataCheckScAdminRankingListReportSource")
public class ExportDataCheckScAdminRankingListReportSource implements IExcelReportSource {

    private Logger logger = LoggerFactory.getLogger(ExportDataCheckScAdminRankingListReportSource.class);

    private String manualData = GetConfigureInfoUtils.getConfigureInfo("file_group") + "_经销商_数据核验排名_销售行政E2";

    @Autowired
    private DataCheckRepository dataCheckRepository;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        Sheet[] sheets = new Sheet[1];
        sheets[0] = new Sheet(manualData,manualData,true);
        return sheets;
    }

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) throws ParseException {
        List<Row> values = new ArrayList<>();

        String permit = context.get("permit") == null ? null : context.get("permit").toString();
        String startDate = context.get("startDate") == null ? null : context.get("startDate").toString();
        String endDate = context.get("endDate") == null ? null : context.get("endDate").toString();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        int dateNum = Integer.parseInt(String.valueOf((sdf.parse(endDate).getTime() - sdf.parse(startDate).getTime()) / (1000 * 3600 * 24))) + 1;

        List list = dataCheckRepository.getScAdminRankingList(permit, startDate, endDate, dateNum);

        values.add(new Row());
        values.add(new Row());

        Cell[] cells = new Cell[1];
        cells[0] = new Cell(startDate + "~" + endDate, 1);
        values.add(new Row(cells));

        values.add(new Row());

        if (list != null && !list.isEmpty()) {
            for (Object o : list) {
                Map<String, Object> map = (Map) o;
                if (manualData.equals(sheet.getName())) {
                    values.add(new Row(getCells(map)));
                }
            }
        }
        return values;
    }

    private Cell[] getCells (Map<String, Object> map) {
        Cell[] cells = new Cell[2];
        int index = 0;
        cells[index++] = new Cell(map.get("distributor") == null ? "" : map.get("distributor").toString(), index);
        cells[index++] = new Cell(map.get("score") == null ? "" : map.get("score").toString(), index);
       return cells;
    }
}
